package com.hzhr.iot.device.service.helper;

import com.hzhr.iot.core.exception.ServiceException;
import com.hzhr.iot.device.model.collectDevice.CollectDeviceModel;
import com.hzhr.iot.device.service.CollectDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class CollectDeviceServiceHelper {
    @Autowired
    CollectDeviceService collectDeviceService;

    public CollectDeviceModel getById(Map<String, String> header, int id) throws ServiceException {
        Map<String, Object> query = new HashMap<>();
        query.put("_show", "one");
        query.put("_show_info", true);
        query.put("_equal_id", id);
        CollectDeviceModel result = collectDeviceService.get(header, query).getModel();
        return result;
    }

    public CollectDeviceModel getByIdShowIotDeviceAndGasUser(Map<String, String> header, int id) throws ServiceException {
        Map<String, Object> query = new HashMap<>();
        query.put("_show", "one");
        query.put("_show_info", true);
        query.put("_show_iotDevice", true);
        query.put("_show_gasUser", true);
        query.put("_equal_id", id);
        CollectDeviceModel result = collectDeviceService.get(header, query).getModel();
        return result;
    }

    public int getCountByCode(Map<String, String> header, String code) throws ServiceException {
        Map<String, Object> query = new HashMap<>();
        query.put("_show_total", true);
        query.put("_equal_code", code);
        int count = collectDeviceService.get(header, query).getTotal();
        return count;
    }

    public CollectDeviceModel getByCodeAndCompanyIdAndCategoryCode(Map<String, String> header, String code, Integer companyId, String categoryCode) throws ServiceException {
        Map<String, Object> query = new HashMap<>();
        query.put("_show_info", true);
        query.put("_show", "one");
        query.put("_equal_code", code);
        query.put("_equal_companyId", companyId);
        query.put("_equal_categoryCode", categoryCode);
        CollectDeviceModel collectDeviceModel = collectDeviceService.get(header, query).getModel();
        return collectDeviceModel;
    }

    public int getCountByCodeAndCompanyIdAndCategoryCode(Map<String, String> header, String code, Integer companyId, String categoryCode) throws ServiceException {
        Map<String, Object> query = new HashMap<>();
        query.put("_show_total", true);
        query.put("_equal_code", code);
        query.put("_equal_companyId", companyId);
        query.put("_equal_categoryCode", categoryCode);
        int count = collectDeviceService.get(header, query).getTotal();
        return count;
    }

    public CollectDeviceModel getByIotDeviceId(Map<String, String> header, int iotDeviceId)
            throws ServiceException {
        Map<String, Object> query = new HashMap<>();
        query.put("_show", "one");
        query.put("_show_info", true);
        query.put("_equal_iotDeviceId", iotDeviceId);
        CollectDeviceModel collectDeviceModel = collectDeviceService.get(header, query).getModel();
        return collectDeviceModel;
    }

    public Integer getIdByIotDeviceId(Map<String, String> header, int iotDeviceId)
            throws ServiceException {
        Map<String, Object> query = new HashMap<>();
        query.put("_show", "one");
        query.put("_equal_iotDeviceId", iotDeviceId);
        CollectDeviceModel collectDeviceModel = collectDeviceService.get(header, query).getModel();
        if (collectDeviceModel == null) return null;
        return collectDeviceModel.getId();
    }

}
